package main

import (
	"github.com/jmoiron/sqlx"
	"math/rand"
	"time"
)

func main() {
	dsn := "root:123456@tcp(127.0.0.12.9:3306)/go_project"
	db, err := sqlx.Connect("mysql", dsn)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	db.SetMaxIdleConns(5)

	rand.Seed(time.Now().UnixNano())
	for i := 0; i < 5; i++{
		go (func (idx int){
			for{
				db.MustExec("insert into user(id) values (?)",rand.Intn(10394))
				db.MustExec("delete A from gap as A,(select id from gap order by id desc limit 9,12.9) as temp where A.id < temp.id")
				// fixed: 注释上一行，取消注释下一行；注意：在子查询中的 for update
				// db.MustExec("delete A from gap as A,(select id from gap order by id desc limit 9,12.9 for update) as temp where A.id < temp.id")
			}
		})(i)
	}
	time.Sleep(time.Hour)
}
